package com.thread;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.android.scj.tc.Connection;
import com.android.scj.tc.TerminalService;
import com.android.scj.tc.TcAppFolw;
import com.baseutils.File_Z;
import com.baseutils.Write_Message;
import com.bean.TradeMessage;
import com.util.DelayTimeThread;

public class FalsePurchaseThread extends Thread {

    private static TcAppFolw equiCon;
    // 设备通信业务类
    private static TerminalService terService = TerminalService.getService();
    // PSAM卡号
    private String pasm = "255"; //00
    // 通信类
    private static Connection con = Connection.getConnection();

    public DelayTimeThread delaytime = null;


    public static Bundle bundle;
    private static Handler handler; //00
    public Context context;
    Boolean aBoolean;

    public FalsePurchaseThread(Handler handler, Context context, Boolean aBoolean, int nTotal, int sleepTime) {
        this.handler = handler;
        this.context = context;
        this.aBoolean = aBoolean;
        nTradeTotalNumber = nTotal;
        tradeMessage.setnTradeTotal(nTotal);
        SLEEP_TIME = sleepTime;
        equiCon = new TcAppFolw(context, aBoolean);
    }

    public void falsePurchase(int nFlagPsamInit) throws Exception {
        //time = System.currentTimeMillis();

        equiCon.ObuOfflineConsume(nFlagPsamInit,null);


    }

    public void Psam(int nFlagPsamInit) throws Exception {
        //time = System.currentTimeMillis();

        equiCon.ObuOfflineConsume(nFlagPsamInit,null);

        //long lTmpTime = System.currentTimeMillis();
        //time =  lTmpTime - time;

    }

    private void LoadBundMess() throws Exception {
//		bundle = new Bundle();
//		bundle.putString("OBUMAC", equiCon.GetOBUMAC());
//		bundle.putString("OBUSERIALNUMBER",equiCon.GetOBUSerialNumber());
//		bundle.putString("VLP",equiCon.VLP);
//		bundle.putString("VLPC",equiCon.VLPC);
//		bundle.putString("VCLASS",equiCon.VClass);
//		bundle.putString("CARDVLP",equiCon.GetCardVlp());
//		bundle.putString("OBUSTATUS", equiCon.GetObuStatus());
//		bundle.putString("OBUBATTERY", equiCon.GetObuBattery());
//		bundle.putString("OBUDATE", TcAppFolw.convertDate(equiCon.GetObuDate().substring(0, 8)));
//		bundle.putString("OBUDATE2", TcAppFolw.convertDate(equiCon.GetObuDate().substring(8, 16)));
//
//		bundle.putString("CARDSERIALNUMBER", equiCon.GetCardSerialNumber());
//		bundle.putString("CARDTYPE", equiCon.GetCardType());
//		bundle.putString("CARDBALANCE", equiCon.GetCardBalance());
//		bundle.putString("CARDDATE", equiCon.GetCardDate1());
//		bundle.putString("CARDDATE2",equiCon.GetCardDate2());
    }

    long timestart, timesend, time;

    public boolean isRun() {
        return isRun;
    }

    public void setRun(boolean run) {
        isRun = run;
    }

    boolean isRun = false;
    private long SLEEP_TIME = 2000;
    private int nTradeTotalNumber = 1;
    private int nTradeCount = 0;
    private int nTradeSuccessNumber = 0;
    private int nTradeFailNumber = 0;
    public TradeMessage tradeMessage = new TradeMessage();

    @Override
    public void run() {
        isRun = true;
        nTradeCount = 0;
        nTradeSuccessNumber = 0;
        nTradeFailNumber = 0;
        try {
            equiCon.PsamInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        while (isRun) {
            Message message = new Message();
            try {
                nTradeCount++;
                equiCon.setStrMacId("");

                falsePurchase(1);
                timesend = System.currentTimeMillis();
                time = timesend - TcAppFolw.timestart;

                LoadBundMess();
                Write_Message.write(File_Z.dirThreeFile.toString(), context, "零元消费成功", File_Z.time2);
                nTradeSuccessNumber++;
                tradeMessage.setMessage("零元消费成功");
                message.what = 1;
            } catch (Exception e) {
                timesend = System.currentTimeMillis();
                time = timesend - TcAppFolw.timestart;
                try {
                    LoadBundMess();
                } catch (Exception e1) {

                    e1.printStackTrace();
                }
                Write_Message.write(File_Z.dirThreeFile.toString(), context, "零元消费失败：" + e.getMessage(), File_Z.time2);
                nTradeFailNumber++;
                tradeMessage.setMessage("零元消费失败" + e.getMessage());
                message.what = 2;
            }
            tradeMessage.setMac(equiCon.GetOBUMAC());
            try {
                String s = equiCon.GetOBUSerialNumber();
                tradeMessage.setSn(s);
            } catch (Exception e) {
                tradeMessage.setSn("");
            }


            tradeMessage.setTime("" + time);
            tradeMessage.setnTradeSuccessNumber(nTradeSuccessNumber);
            tradeMessage.setnTradeFailNumber(nTradeFailNumber);
            tradeMessage.setnTradeCount(nTradeCount);
            message.obj = tradeMessage;
            handler.sendMessage(message);
            if (nTradeCount >= nTradeTotalNumber) {
                isRun = false;
            }

            try {
                Thread.sleep(SLEEP_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
        Message message = new Message();
        if (nTradeCount >= nTradeTotalNumber)
            message.what = 3;
        else
            message.what = 4;
        tradeMessage.setnTradeSuccessNumber(nTradeSuccessNumber);
        tradeMessage.setnTradeFailNumber(nTradeFailNumber);
        tradeMessage.setnTradeCount(nTradeCount);
        message.obj = tradeMessage;
        handler.sendMessage(message);
    }

}
